using micromsg;
using MicroMsg.Common.Algorithm;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Protocol;
using MicroMsg.Scene;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
namespace MicroMsg.Plugin.DriftBottle.Scene
{
	public class NetSceneThrowBottle : NetSceneBaseEx<ThrowBottleRequest, ThrowBottleResponse, ThrowBottleRequest.Builder>
	{
		private const string TAG = "NetSceneThrowBottle";
		private ThrowVoiceBottleContext mVoiceContext;
		private int mMsgType;
		private byte[] mOutputBuffer;
		private string mStrClientId = "";
		private string mContent = "";
		private object mOutputBufLock = new object();
		public bool doTextBottleScene(string string_0)
		{
			this.mMsgType = 1;
			this.mStrClientId = MD5Core.GetHashString(string.Concat(Util.getNowMilliseconds()));
			this.mContent = string_0;
			return this.doSceneEx(Util.StringToByteArray(string_0), this.mMsgType);
		}
		public void doVoiceScene(ThrowVoiceBottleContext context)
		{
			if (context == null)
			{
				return;
			}
			if (context.mStatus != 0 && context.mStatus != 4)
			{
				Log.smethod_1("NetSceneThrowBottle", "Throw Bottle doScene status error, status = " + context.mStatus);
				return;
			}
			Log.smethod_5("NetSceneThrowBottle", "Throw Bottle netscene startup. ");
			context.mStatus = 1;
			this.mVoiceContext = context;
			this.mMsgType = 3;
			this.mStrClientId = this.mVoiceContext.mClientMsgID;
			if (context.isInvalidShortVoice())
			{
				context.mStatus = 4;
				Log.smethod_1("NetSceneThrowBottle", "complete with short voice , ignored.. ");
				this.doSceneFinished(3);
				return;
			}
			byte[] bufferFromHead = this.mVoiceContext.getBufferFromHead();
			if (bufferFromHead == null)
			{
				context.mStatus = 4;
				Log.smethod_1("NetSceneThrowBottle", "mVoiceContext.getBufferFromHead() return null");
				EventCenter.postEvent(EventConst.ON_NETSCENE_GRIFTBOTTLE_EVENT, new ThrowBottleRet(BottleEvent.ON_NETSCENE_THROWBOTTLE_FAIL, RetConst.MM_ERR_SYS), null);
				return;
			}
			Log.smethod_5("NetSceneThrowBottle", "first voice buf length = " + bufferFromHead.Length);
			this.doSceneEx(bufferFromHead, this.mMsgType);
		}
		private bool doSceneEx(byte[] contentBuf, int msgType)
		{
			if (contentBuf == null)
			{
				if (this.mVoiceContext != null)
				{
					this.mVoiceContext.mStatus = 4;
				}
				Log.smethod_1("NetSceneThrowBottle", "doSceneEx contentBuf == null");
				return false;
			}
			base.beginBuilder();
			this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(0);
			this.mBuilder.MsgType = msgType;
			this.mBuilder.BottleType = 0;
			this.mBuilder.Content = Util.toSKBuffer(contentBuf);
			this.mBuilder.ClientID = this.mStrClientId;
			if (msgType == 3 && this.mVoiceContext != null)
			{
				this.appendOutputData(contentBuf, this.mVoiceContext.mNetOffset, contentBuf.Length);
				this.mBuilder.StartPos = this.mVoiceContext.mNetOffset;
				this.mBuilder.TotalLen = this.mVoiceContext.mOutputLength;
				Log.smethod_5("NetSceneThrowBottle", "mVoiceContext.mOutputLength = " + this.mVoiceContext.mOutputLength);
				this.mBuilder.VoiceLength = (uint)this.mVoiceContext.mVoiceTimeLength;
			}
			else
			{
				if (msgType != 1)
				{
					Log.smethod_1("NetSceneThrowBottle", "doSceneEx msgType error");
					if (this.mVoiceContext != null)
					{
						this.mVoiceContext.mStatus = 4;
					}
					return false;
				}
				this.mBuilder.StartPos = 0;
				this.mBuilder.TotalLen = contentBuf.Length;
				this.mBuilder.VoiceLength = 0u;
			}
			this.mSessionPack.mCmdUri = "/cgi-bin/micromsg-bin/throwbottle";
			this.mSessionPack.mConnectMode = 2;
			base.endBuilder();
			return true;
		}
		protected override void onFailed(ThrowBottleRequest request, ThrowBottleResponse response)
		{
			Log.smethod_1("NetSceneThrowBottle", "Throw bottle failed system error!");
			if (this.mVoiceContext != null)
			{
				this.mVoiceContext.mStatus = 4;
			}
			EventCenter.postEvent(EventConst.ON_NETSCENE_GRIFTBOTTLE_EVENT, new ThrowBottleRet(BottleEvent.ON_NETSCENE_THROWBOTTLE_FAIL, RetConst.MM_ERR_SYS), null);
		}
		protected override void onSuccess(ThrowBottleRequest request, ThrowBottleResponse response)
		{
			if (response == null || response.BaseResponse.Ret != 0)
			{
				Log.smethod_1("NetSceneThrowBottle", "Throw Bottle response not invalidate Ret = " + ((RetConst)response.BaseResponse.Ret).ToString());
				RetConst serverRet = (RetConst)response.BaseResponse.Ret;
				if (response.BaseResponse.Ret == 19)
				{
					serverRet = RetConst.MM_ERR_NO_BOTTLECOUNT;
				}
				DriftBottleServiceCenter.setBottleCount(response.PickCount, response.ThrowCount);
				EventCenter.postEvent(EventConst.ON_NETSCENE_GRIFTBOTTLE_EVENT, new ThrowBottleRet(BottleEvent.ON_NETSCENE_THROWBOTTLE_FAIL, serverRet), null);
				if (this.mVoiceContext != null)
				{
					this.mVoiceContext.mStatus = 4;
				}
				return;
			}
			if (this.mMsgType != 3 || this.mVoiceContext == null)
			{
				if (this.mMsgType == 1)
				{
					DriftBottleServiceCenter.setBottleCount(response.PickCount, response.ThrowCount);
					Log.smethod_4("NetSceneThrowBottle", "Throw Text bottle success!");
					this.saveThrowBottle(response);
					EventCenter.postEvent(EventConst.ON_NETSCENE_GRIFTBOTTLE_EVENT, new ThrowBottleRet(BottleEvent.ON_NETSCENE_THROWBOTTLE_SUCCESS, (RetConst)response.BaseResponse.Ret), null);
				}
				return;
			}
			Log.smethod_5("NetSceneThrowBottle", "mVoiceContext.mNetOffset = " + this.mVoiceContext.mNetOffset);
			Log.smethod_5("NetSceneThrowBottle", "mVoiceContext.mSendingLength = " + this.mVoiceContext.mSendingLength);
			Log.smethod_5("NetSceneThrowBottle", "mVoiceContext.mOutputLength = " + this.mVoiceContext.mOutputLength);
			this.mVoiceContext.mNetOffset = this.mVoiceContext.mNetOffset + this.mVoiceContext.mSendingLength;
			this.mVoiceContext.mSendingLength = 0;
			if (this.mVoiceContext.mNetOffset < this.mVoiceContext.mOutputLength)
			{
				byte[] bufferFromHead = this.mVoiceContext.getBufferFromHead();
				Log.smethod_5("NetSceneThrowBottle", "send voice buf length = " + bufferFromHead.Length);
				this.doSceneEx(bufferFromHead, this.mMsgType);
				return;
			}
			DriftBottleServiceCenter.setBottleCount(response.PickCount, response.ThrowCount);
			if (!this.saveThrowBottle(response))
			{
				Log.smethod_1("NetSceneThrowBottle", "saveThrowBottle failed");
				this.mVoiceContext.mStatus = 4;
				EventCenter.postEvent(EventConst.ON_NETSCENE_GRIFTBOTTLE_EVENT, new ThrowBottleRet(BottleEvent.ON_NETSCENE_THROWBOTTLE_FAIL, RetConst.MM_ERR_SYS), null);
				return;
			}
			Log.smethod_4("NetSceneThrowBottle", "Throw voice bottle success!");
			this.mVoiceContext.mStatus = 5;
			EventCenter.postEvent(EventConst.ON_NETSCENE_GRIFTBOTTLE_EVENT, new ThrowBottleRet(BottleEvent.ON_NETSCENE_THROWBOTTLE_SUCCESS, RetConst.MM_OK), null);
		}
		private bool saveThrowBottle(ThrowBottleResponse resp)
		{
			if (resp == null)
			{
				return false;
			}
			long nCreateTime = (long)Util.getNowSeconds();
			if (resp.BottleListList == null || resp.BottleListCount <= 0)
			{
				return false;
			}
			Log.smethod_4("NetSceneThrowBottle", "resp.BottleListCount = " + resp.BottleListCount);
			List<MicroMsg.Storage.BottleInfo> list = new List<MicroMsg.Storage.BottleInfo>();
			string text = null;
			if (this.mMsgType == 3)
			{
				text = this.saveVoiceTofile();
			}
			for (int i = 0; i < resp.BottleListCount; i++)
			{
				Log.smethod_5("NetSceneThrowBottle", resp.BottleListList.get_Item(i).ToString());
				string text2 = Util.ByteArrayToString(resp.BottleListList.get_Item(i).ToByteArray());
				if (text2 != null)
				{
					string[] array = text2.Split(new char[]
					{
						':'
					});
					if (array != null && array.Length > 1)
					{
						MicroMsg.Storage.BottleInfo bottleInfo = new MicroMsg.Storage.BottleInfo();
						bottleInfo.nCreateTime = nCreateTime;
						bottleInfo.strBottleID = array[1];
						bottleInfo.strUserName = array[0];
						bottleInfo.nIsSender = 1;
						bottleInfo.nMsgType = NetSceneOpenBottle.bottleType2ChatMsgType(this.mMsgType);
						bottleInfo.nChildCount = 0;
						if (this.mMsgType == 1)
						{
							bottleInfo.strContent = this.mContent;
						}
						else
						{
							if (string.IsNullOrEmpty(text))
							{
								return false;
							}
							bottleInfo.strContent = NetSceneOpenBottle.getBottleContent(this.mVoiceContext.mOutputLength, this.mVoiceContext.mVoiceTimeLength, "self@bottle:self");
							bottleInfo.strVoicelen = string.Concat(this.mVoiceContext.mVoiceTimeLength);
							bottleInfo.strFilePath = text;
						}
						bottleInfo.strParentClientid = MD5Core.GetHashString(text2);
						list.Add(bottleInfo);
					}
				}
			}
			Log.smethod_5("NetSceneThrowBottle", "bottleIDList.Count" + list.get_Count());
			if (list.get_Count() > 0)
			{
				if (list.get_Count() > 12)
				{
					DriftBottleServiceCenter.maxCopyCount = list.get_Count();
				}
				StorageMgr.bottle.addList(list);
				return true;
			}
			Log.smethod_1("NetSceneThrowBottle", "ThrowBottle save nothing");
			return false;
		}
		private void doSceneFinished(int finishCode)
		{
		}
		public string saveVoiceTofile()
		{
			if (this.mOutputBuffer == null)
			{
				Log.smethod_1("NetSceneThrowBottle", "mOutputBuffer == null");
				return null;
			}
			string hashString = MD5Core.GetHashString(this.mVoiceContext.mOutputLength + Util.getNowMilliseconds());
			string text = StorageIO.getPluginPath(DriftBottle.getInstance().mName) + "/voice/" + hashString + ".amr";
			Log.smethod_5("NetSceneThrowBottle", "voiceFileName = " + text);
			if (string.IsNullOrEmpty(text))
			{
				Log.smethod_1("NetSceneThrowBottle", "voiceFileName is null");
				return null;
			}
			if (!NetSceneOpenBottle.writeVoiceToFile(text, this.mOutputBuffer))
			{
				Log.smethod_1("NetSceneThrowBottle", "writeVoiceToFile fail");
				return null;
			}
			return text;
		}
		public bool appendOutputData(byte[] buffer, int offset, int count)
		{
			bool result;
			lock (this.mOutputBufLock)
			{
				if (count > 0 && buffer != null)
				{
					if (this.mOutputBuffer == null)
					{
						this.mOutputBuffer = new byte[count];
					}
					if (this.mOutputBuffer.Length < offset + count)
					{
						int num = this.mOutputBuffer.Length;
						byte[] array = this.mOutputBuffer;
						this.mOutputBuffer = new byte[offset + count];
						Buffer.BlockCopy(array, 0, this.mOutputBuffer, 0, num);
					}
					Buffer.BlockCopy(buffer, 0, this.mOutputBuffer, offset, count);
					result = true;
				}
				else
				{
					result = true;
				}
			}
			return result;
		}
	}
}
